package com.thank.concurrency.samples.immutable;

import com.google.common.collect.Maps;
import com.thank.concurrency.annoations.NotThreadSafe;
import com.thank.concurrency.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 演示java.util.Collections下的不可变集合构造
 */
@Slf4j
@ThreadSafe
public class ImmutableExample2 {

    private static Map<Integer, Integer> map = Maps.newHashMap();

    static {
        map.put(1, 10);
        map.put(2, 20);
        map.put(3, 30);
        map = Collections.unmodifiableMap(map);
    }

    public static void main(String[] args) {
        // 编译通过, 但是修改值会抛出UnsupportedOperationException
        map.put(1, 100);
    }

}
